/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mkt.view;

import com.sun.org.apache.xml.internal.serializer.utils.StringToIntTable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import mkt.classes.DAO.AvaliacaoDAO;
import mkt.classes.bean.Avaliacao;
import mkt.classes.bean.Usuario;
import mkt.classes.bean.UsuarioControle;

/**
 *
 * @author luis
 */
public class FormRecomenda extends javax.swing.JInternalFrame {

    public final TrabalhoMKTMDI mdi;
    public double media;
    
    /**
     * Creates new form FormRecomenda
     */
    public FormRecomenda(TrabalhoMKTMDI mdi) {
        this.mdi = mdi;
        initComponents();
        mdi.setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();

        setClosable(true);
        setIconifiable(true);
        setTitle("Itens Recomendados");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Talvez você tenha interesse em:"));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 527, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 164, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 535, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 113, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    public double geraMedia(int soma, int quantidade){
        media = (soma / quantidade);
        return media;
    }
    
    
    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        try {
            AvaliacaoDAO avaliacaoDAO = new AvaliacaoDAO(TrabalhoMKTMDI.getConexao());
            Usuario usuario = new Usuario();
            List<Avaliacao> listaAvaliacoes = avaliacaoDAO.listaTodos();
            LinkedList listaRecomendacao = new LinkedList();
            int notasUsuario1[] = new int[20];
            int notasUsuario2[] = new int[20];
            int indice = 0;
            int soma1 = 0, soma2 = 0;
            int quantidadeAvaliacoes1 = 0,quantidadeAvaliacoes2 = 0;

            for (Avaliacao avaliacao : listaAvaliacoes) {
                while(UsuarioControle.getUsuario().getCod() != avaliacao.getCodUsuario()) {
                    int usuario1 = UsuarioControle.getUsuario().getCod();
                    int usuario2 = avaliacao.getCodUsuario();
                    
                    if (usuario1 != usuario2) {
                        //armazena notas do usuario 1
                        for(int i = 0; (i<=listaAvaliacoes.size()); i++){
                            indice = 0;
                            if(avaliacao.getCodUsuario() == usuario1){
                                notasUsuario1[indice] = avaliacao.getNota();
                                indice++;
                                soma1 = soma1 + notasUsuario1[indice];
                                quantidadeAvaliacoes1++;
                            }
                        }
                        //armazena notas do usuario 2
                        for(int i = 0; (i<=listaAvaliacoes.size()); i++){
                            indice = 0;
                            if(avaliacao.getCodUsuario() == usuario2){
                                notasUsuario2[indice] = avaliacao.getNota();
                                indice++;
                                soma2 = soma2 + notasUsuario2[indice];
                                quantidadeAvaliacoes2++;;
                            }
                        }
                        //calculando item e média de cada usuario;
                        //sigla u = usuario, / l = livro / m = média 
                        double u1l1m1 = (notasUsuario1[indice]) - (geraMedia(soma1, quantidadeAvaliacoes1));
                        double u2l1m2 = (notasUsuario2[indice]) - (geraMedia(soma2, quantidadeAvaliacoes2));
                        //calculando similaridade entre os usuarios;
                        double calculo1 = (u1l1m1) * (u2l1m2) + (5 - 8) * (5 - 7.33) + (9 - 8) * (8 - 7.33);
                        
                        
                        //double calculo1 = (10 - 8) * (9 - 7.33) + (5 - 8) * (5 - 7.33) + (9 - 8) * (8 - 7.33);
                        //double calculo2 = Math.pow(10 - 8, 2) + Math.pow(5 - 8, 2) + Math.pow(9 - 8, 2);
                        //double calculo3 = Math.pow(9 - 7.33, 2) + Math.pow(5 - 7.33, 2) + Math.pow(8 - 7.33, 2);
                        //double calculo4 = Math.sqrt(calculo2) * Math.sqrt(calculo3);
                        //double calculo5 = calculo1 / calculo4;
                        
                    } else {
                        //usuarios iguais! problema!
                        break;
                    }
                }
            }
    
        } catch (SQLException ex) {
            System.out.println("ERRO ao recomendar livros");
            Logger.getLogger(FormAvaliacao.class.getName()).log(Level.WARNING, "Erro ao ler recomendações", ex);
        }
       
    }//GEN-LAST:event_formInternalFrameOpened
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables
}
